package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daxidi.cn.entity.Bill;
import com.daxidi.cn.entity.Shop;
import com.daxidi.cn.mapper.BillMapper;
import com.daxidi.cn.mapper.ShopMapper;
import com.daxidi.cn.service.IShopService;
import com.daxidi.cn.vo.ShopBillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商铺表 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private BillMapper billMapper;

    @Override
    public void addShop(Shop shop) {
        shopMapper.insert(shop);
    }

    @Override
    public void deleteShopById(Long id) {
        shopMapper.deleteById(id);
    }

    @Override
    public void updateShopById(Shop shop) {
        shopMapper.updateById(shop);
    }

    @Override
    public Shop selectShop(Long id) {
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Shop shop = shopMapper.selectOne(wrapper);
        shop.setUrl(shop.getIcon());
        return shop;
    }

    // 账单详情
    @Override
    public ShopBillVo selectShopBillVo(Integer id) {
        ShopBillVo shopBillVo = shopMapper.selectShopBillVoById(id);
        return shopBillVo;
    }

    // 根据商家名称 模糊搜索商家信息
    @Override
    public List<ShopBillVo> selectLikeShops(String shopName) {
        List<ShopBillVo> shopLikeVos = shopMapper.selectLikeShops(shopName);
        return shopLikeVos;
    }

    //  商家列表展示
    @Override
    public List<ShopBillVo> selectShops() {
        List<ShopBillVo> shopLikeVos = shopMapper.selectShops();
        return shopLikeVos;
    }

    @Override
    public String getSubMchId(Long orderId) {
        //根据orderId查shopId
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        Bill bill = billMapper.selectOne(wrapper);
        //根据shopId查询subMchId
        return shopMapper.selectById(bill.getShopId()).getSubMchId();
    }

    @Override
    public Map searchShop(Integer page, Integer limit, String boss, String phone, String beginTime, String endTime, String enable) {
        Page<Shop> shopVoPage = new Page<>(page, limit);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        if (!"".equals(boss)) {
            wrapper.like(boss != null, "name", boss);
        }
        if (!"".equals(phone)) {
            wrapper.like(phone != null, "phone", phone);
        }
        wrapper.between(beginTime != null && beginTime != "" && endTime != null && endTime != "", "created_at", beginTime, endTime);
        /*if (!"".equals(endTime)){
            wrapper.le(endTime != null, "valid_at", endTime);
        }*/
        wrapper.eq("enable", enable);
        Page<Shop> shopPage = shopMapper.selectPage(shopVoPage, wrapper);
        List<Shop> shops = shopPage.getRecords();
        long total = shopPage.getTotal();
        Map map = new HashMap();
        map.put("data", shops);
        map.put("count", total);
        return map;
    }


}
